<?php
class EavValue extends EavAppModel {

	var $name = 'EavValue';
	var $useTable = false;
	
	function saveValue($eavRecordId, $field, $value)
	{
		//echo $eavRecordId; exit;
		$this->deleteAll(array(
			$this->alias.'.eav_record_id' => $eavRecordId,
			$this->alias.'.field' => $field,
		));
		
		$this->id = '';
		$record = array($this->alias => array(
			'eav_record_id' => $eavRecordId,
			'field' => $field,
			'value' => $value
		));
		return $this->save($record);
	}
	
	/**
	 * returns records for given field's value.
	 * @param $field
	 * @param $value
	 * @param $opreator
	 */
	function getConditionRecords($field,$value,$opreator = null,$entity = false)
	{
		$this->bindModel(array('belongsTo'=>array('EavRecord'=>array('className'=>'Eav.EavRecord'))),false);
		
		return $this->findConditionRecords($field,$value,$opreator,$entity);
	}
	/**
	 * Get Eav Records that matches the condition
	 */
	protected  function findConditionRecords($field,$value,$opreator = null,$entity = false,$options = array())
	{
		$space = " ";
		if($opreator == null)
		{
			$space = "";
		}
		$condition = array();
		$condition = array($this->alias.'.field'=>$field,$this->alias.'.value'.$space.$opreator => $value);
		if($entity)
		{
			$condition[$this->EavRecord->alias.'.entity'] = $entity;
		}
		$options = am(array('recursive'=>0),$options);
		$options['conditions'] = $condition;
		return $this->find('all',$options);
	}

}

class EavValuesBoolean extends EavValue 
{
	var $name = 'EavValuesBoolean';
	var $useTable = "eav_values_boolean";
}
class EavValuesDatetime extends EavValue 
{
	var $name = 'EavValuesDatetime';
	var $useTable = "eav_values_datetime";
}
class EavValuesDate extends EavValue 
{
	var $name = 'EavValuesDate';
	var $useTable = "eav_values_date";
}
class EavValuesInt extends EavValue 
{
	var $name = 'EavValuesInt';
	var $useTable = "eav_values_int";
}
class EavValuesString extends EavValue 
{
	var $name = 'EavValuesString';
	var $useTable = "eav_values_string";
}
class EavValuesText extends EavValue 
{
	var $name = 'EavValuesText';
	var $useTable = "eav_values_text";
}
class EavValuesFloat extends EavValue 
{
	var $name = 'EavValuesFloat';
	var $useTable = "eav_values_float";
	var $fieldProperties = array("value"=>array("fieldModifier"=>"parseFloatValue"));
	function parseFloatValue($value,$action)
	{
		if($action == self::READ && $value)
		{
			$value = floatval($value);
		}
		return $value;
	}
}
class EavValuesArray extends EavValue 
{
	var $name = 'EavValuesArray';
	var $useTable = "eav_values_array";
	
	var $hasMany = array('EavValuesArrayElement');
	
	var $additionalFields = array('value');
	
	function value($record)
	{
		if(isset($record['EavValuesArrayElement']))
		{
			$values = Set::extract('{n}.value', $record['EavValuesArrayElement']);
			return $values;
		}
		return array();
	}
	
	function saveValue($eavRecordId, $field, $value)
	{
		if(!is_array($value))
		{
			return false;
		}

		$this->deleteAll(array(
			$this->alias.'.eav_record_id' => $eavRecordId,
			$this->alias.'.field' => $field,
		));
		
		$this->id = '';
		$record = array($this->alias => array(
			'eav_record_id' => $eavRecordId,
			'field' => $field
		));
		$return = $this->save($record);
		if(!$return)
		{
			return false;
		}
		foreach($value as $val)
		{
			$arrayElement = array('EavValuesArrayElement' => array(
				'value' => (string)$val,//type casting to string else if value is 0 int then it causes problem by saving as CURRENT_TIMESTAMP instead of original value.
				'eav_values_array_id' => $this->id
			));
			$this->EavValuesArrayElement->id = '';
			$this->EavValuesArrayElement->create();
			$return = $this->EavValuesArrayElement->save($arrayElement);
			if(!$return)
			{
				return false;
			}
		}		
		
		return true;
	}
	
	protected  function findConditionRecords($field,$value,$opreator = null,$entity = false,$options = array())
	{
		$records = $this->EavValuesArrayElement->getConditionRecords($field,$value,$opreator);
		$recordsIds = Set::extract('{n}.'.$this->alias.'.'.$this->EavRecord->foreignKey,$records);
		$conditions = array($this->EavRecord->alias.'.'.$this->EavRecord->primaryKey=>$recordsIds);
		if($entity!==false)
		{
			$conditions[$this->EavRecord->alias.'.entity'] = $entity;
		}
		$eavRecords = $this->EavRecord->find('all',array('conditions'=>$conditions,'recursive'=>-1));
		return $eavRecords;
	}
}
class EavValuesArrayElement extends EavValue 
{
	var $name = 'EavValuesArrayElement';
	var $useTable = "eav_values_array_elements";
	
	function getConditionRecords($field,$value,$opreator)
	{
		$this->bindModel(array('belongsTo'=>array('EavValuesArray'=>array('className'=>'Eav.EavValuesArray'))));
		return $this->findConditionRecords($field,$value,$opreator);
	}
	
	/**
	 * Get Eav Records that matches the condition
	 */
	protected  function findConditionRecords($field,$value,$opreator = null,$options = array())
	{
		$space = " ";
		if($opreator == null)
		{
			$space = "";
		}
		$condition = array();
		$condition = array($this->EavValuesArray->alias.'.field'=>$field,$this->alias.'.value'.$space.$opreator => $value);
		$options = am(array('recursive'=>0),$options);
		$options['conditions'] = $condition;
		return $this->find('all',$options);
	}
}
?>